package com.sikao.app.ui;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.sikao.app.R;
import com.sikao.app.bean.Bean;
import com.sikao.app.bean.GetSmsSubmitBean;
import com.sikao.app.bean.RetrieveLoginPswSubmitBean;
import com.sikao.app.dao.GraphicCodeModel;
import com.sikao.app.dao.RetrieveLoginPswModel;
import com.sikao.app.dao.SmsCodeImgModel;
import com.sikao.app.net.OnNetBackListener;
import com.sikao.app.ui.base.BaseActivity;
import com.sikao.app.utils.AndroidBug5497Workaround;
import com.sikao.app.utils.JSONUtil;
import com.sikao.app.utils.StringUtils;
import com.sikao.app.utils.TimeCountUtil;

import static android.R.attr.data;

/**
 * 找回登录密码
 */
public class RetrieveLoginPswActivity extends BaseActivity implements View.OnClickListener {

	private TextView titleTv;
	private ImageView backIv;
	private EditText phoneEt;
	private EditText imgCodeEt;
	private ImageView imgCodeIv;
	private EditText smsCodeEt;
	private Button smsCodeBtn;
	private EditText passwordEt1;
	private EditText passwordEt2;
	private Button resetPwsBtn;
	private GetSmsSubmitBean bean;
	private ImageLoader imageLoader = ImageLoader.getInstance();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_retrieve_login_psw);
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void initData() {
	}

	@Override
	protected void initView() {
		titleTv = (TextView) findViewById(R.id.title_middle_tv);
		backIv = (ImageView) findViewById(R.id.title_left_img);

		phoneEt = (EditText) findViewById(R.id.phone_et);
		imgCodeEt = (EditText) findViewById(R.id.img_code_et);
		imgCodeIv = (ImageView) findViewById(R.id.img_code_iv);
		smsCodeEt = (EditText) findViewById(R.id.sms_code_et);
		smsCodeBtn = (Button) findViewById(R.id.sms_code_btn);
		passwordEt1 = (EditText) findViewById(R.id.password_et1);
		passwordEt2 = (EditText) findViewById(R.id.password_et2);
		resetPwsBtn = (Button) findViewById(R.id.reset_pws_btn);
	}

	@Override
	protected void initListener() {
		backIv.setOnClickListener(this);
		imgCodeIv.setOnClickListener(this);
		smsCodeBtn.setOnClickListener(this);
		resetPwsBtn.setOnClickListener(this);
	}

	@Override
	protected void init() {
		titleTv.setText("找回登录密码");
		backIv.setVisibility(View.VISIBLE);
		getGraphicCode();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.title_left_img:
				finish();
				break;
			case R.id.img_code_iv:
				getGraphicCode();
				break;
			case R.id.sms_code_btn:
				getSmsCode();
				break;
			case R.id.reset_pws_btn:
				resetPws();
				break;
			default:
				break;
		}
	}

	private void resetPws() {
		if (!checkInput()) {
			return;
		}
		RetrieveLoginPswSubmitBean bean = new RetrieveLoginPswSubmitBean();
		bean.setPhone(phoneEt.getText().toString());
		bean.setPhoneCode(smsCodeEt.getText().toString());
		bean.setType("forgetLoginPwd");
		bean.setPwd(passwordEt1.getText().toString());
		bean.setAgapwd(passwordEt2.getText().toString());
		bean.setId("1");
		new RetrieveLoginPswModel(this, new OnNetBackListener<Bean>() {
			@Override
			public void netBack(Bean data) {
				if (data == null) {
					return;
				}
				if (data.getStatus().equals("1")) {
					Toast.makeText(RetrieveLoginPswActivity.this, "新密码设置成功", Toast.LENGTH_SHORT).show();
					finish();
				} else {
					Toast.makeText(RetrieveLoginPswActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
				}
			}
		}).request(JSONUtil.toJSON(bean), true);
	}

	private boolean checkInput() {

		if (TextUtils.isEmpty(phoneEt.getText().toString())) {
			Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
			return false;
		}
		/*if (!StringUtils.isMobile(phoneEt.getText().toString())) {
			Toast.makeText(this, "手机号格式错误", Toast.LENGTH_SHORT).show();
			return false;
		}*/
		if (TextUtils.isEmpty(smsCodeEt.getText().toString())) {
			Toast.makeText(this, "请输入短信验证码", Toast.LENGTH_SHORT).show();
			return false;
		}
		if (TextUtils.isEmpty(passwordEt1.getText().toString())) {
			Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
			return false;
		}
		if (!StringUtils.isLoginPsw(passwordEt1.getText().toString())) {
			Toast.makeText(this, "密码不符合要求", Toast.LENGTH_SHORT).show();
			return false;
		}
		if (!(passwordEt1.getText().toString()).equals(passwordEt2.getText().toString())) {
			Toast.makeText(this, "两次密码不一致", Toast.LENGTH_SHORT).show();
			return false;
		}
		return true;
	}

	private void getSmsCode() {
		if (TextUtils.isEmpty(phoneEt.getText().toString())) {
			Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
			return;
		}
		/*if (!StringUtils.isMobile(phoneEt.getText().toString())) {
			Toast.makeText(this, "手机号格式错误", Toast.LENGTH_SHORT).show();
			return;
		}*/
		if (TextUtils.isEmpty(imgCodeEt.getText().toString())) {
			Toast.makeText(this, "请输入图形验证码", Toast.LENGTH_SHORT).show();
			return;
		}
		bean = new GetSmsSubmitBean();
		bean.setType("forgetLoginPwd");
		bean.setCode(imgCodeEt.getText().toString());
		bean.setPhone(phoneEt.getText().toString());
		bean.setId("1");
		new SmsCodeImgModel(this, new OnNetBackListener<Bean>() {
			@Override
			public void netBack(Bean smsBean) {
				if (smsBean.getStatus().equals("1")) {
					Toast.makeText(RetrieveLoginPswActivity.this, "成功发送", Toast.LENGTH_SHORT).show();
					TimeCountUtil timeCountUtil = new TimeCountUtil(60000, 1000, smsCodeBtn);
					timeCountUtil.start();
				} else {
					Toast.makeText(RetrieveLoginPswActivity.this, smsBean.getMsg(), Toast.LENGTH_SHORT).show();
				}
			}
		}).request(JSONUtil.toJSON(bean), false);

	}

	private void getGraphicCode() {
		new GraphicCodeModel(this, new OnNetBackListener<Bitmap>() {
			@Override
			public void netBack(Bitmap bm) {
				if (bm != null) {
					imgCodeIv.setImageBitmap(bm);
				} else {
					imgCodeIv.setImageDrawable(getResources().getDrawable(R.mipmap.img_code_fail));
				}
			}
		}).request("forgetLoginPwd", false);
	}
}
